//
//  AYMLoginViewController.m
//  QuickCure
//
//  Created by MootYue on 16/5/5.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMLoginViewController.h"
#import "SVPAlertManager.h"
#import "UserData.h"
#import "AYMHomeController.h"
#import "AYMFindPSWController.h"
#import "AYMRegisterViewController.h"
#import "AYMForceTouchClik.h"


@interface AYMLoginViewController ()
//用户名Lable
@property (weak, nonatomic) IBOutlet UITextField *acountTextField;
//密码Lable
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (nonatomic, strong) AYMHomeController *homeVC;
@end

@implementation AYMLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //给文本框添加左边图片
    [self setLeftImage:self.acountTextField leftImageNamed:@"product_img_icon_xieyi.png"];
    [self setLeftImage:self.passwordTextField leftImageNamed:@"product_img_icon_xieyi.png"];
    [self.view setBackgroundColor:[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1]];

}


//点击登录
- (IBAction)LoginClick:(id)sender {
    
   //用户名密码没有输入或者输入有误的情况
    [self isRight];
    
    //用户名密码正确
    if ([self.acountTextField.text isEqualToString:@"11"] && [self.passwordTextField.text isEqualToString:@"11"]) {
        
        //检查是否能连接
        if ([AYMNetworkTool sharedManager].isReachableByAFN) {
            //    请求地址: http://iosapi.itcast.cn/carelinkQuickLogin.json.php
            //    请求参数:{"login_token":"985c96ce43bb5ef69de3e1ad8afaa83f"}
            //    登录成功后，用户id为：1000089
            NSString *urlString = @"http://iosapi.itcast.cn/carelinkQuickLogin.json.php";

            NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};

            
            //Constant中宏定义了
            WEAKSELF
          [[AYMNetworkTool sharedManager] POST:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                [SVPAlertManager alertSuccessStatus:@"登录成功"];
              
                //设置一个用户数据isUserLoginKey 为Bool 用来记录登陆
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isUserLogiKey"];
                [AYMForceTouchClik loginForceTouch];
              
                //取出数据
                NSDictionary *dic = responseObject[@"data"];
                //从字典中取值
                UserData *userDat = [[UserData sharedInstance] setDataFromDict:dic];
                userDat.isLogin = YES;
                //保存到沙盒
                [[UserData sharedInstance] saveUserInfoSanBox:userDat];
              
               //回调的block
                weakSelf.loginBlock();
                //延迟返回主界面
                [self performSelector:@selector(loginPass) withObject:nil afterDelay:1.2];

                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                
            }];


        }else{
            [SVPAlertManager alertErrorStatus:@"请检查网络"];
        }

    }
        
   
}
//返回主页
-(void)loginPass{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


//点击注册新账号按钮
- (IBAction)registClickButtom:(id)sender {
    AYMRegisterViewController *registVC = [AYMRegisterViewController new];
    [self jumpToTargetVC:registVC];
}

//点击忘记密码按钮
- (IBAction)losePassWordBottm:(id)sender {
    AYMFindPSWController *findPSWVC = [[AYMFindPSWController alloc]init];
    [self jumpToTargetVC:findPSWVC];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//判断用户名账号密码
-(void)isRight{
    //检查用户名是否输入
    if (self.acountTextField.text.length == 0) {
        
        [SVPAlertManager alertErrorStatus:@"请输入用户名"];
        return;
    }
    //检查密码是否输入
    if (self.passwordTextField.text.length == 0) {
        
        [SVPAlertManager alertErrorStatus:@"请输密码"];
        return;
    }
    //判断用户名密码是否正确
    if (![self.acountTextField.text isEqualToString:@"11"] || ![self.passwordTextField.text isEqualToString:@"11"]) {
        [SVPAlertManager alertErrorStatus:@"用户名密码错误请重新输入"];
    }
    
}


- (void)setLeftImage:(UITextField *)textField leftImageNamed:(NSString *)imageName
{
    // 初始化imageView
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:imageName];
    leftView.frame = CGRectMake(0, 0, 30, 30);
    
    // 设置内容居中
    leftView.contentMode = UIViewContentModeCenter;
    textField.leftView = leftView;
    
    // 设置左边的image永远显示
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    // 设置右边永远显示 X
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
